package com.dsg.controller.oil;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.oil.DsgOilCardtype;
import com.dsg.entity.oil.DsgOilInfo;
import com.dsg.service.oil.IDsgOilAccountService;
import com.dsg.service.oil.IDsgOilCardtypeService;
import com.dsg.service.oil.IDsgOilInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/oil/dsgOilInfo")
@Slf4j
@Api(description = "油公司資料")
public class DsgOilInfoController extends BaseController {

    @Autowired
    private IDsgOilInfoService dsgOilInfoService;
    @Autowired
    private IDsgOilCardtypeService dsgOilCardtypeService;
    @Autowired
    private IDsgOilAccountService dsgOilAccountService;

    /**
     * 查詢列表（分頁）
     *
     * @param dsgOilInfo
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查詢列表（分頁）")
    public AjaxResult list(DsgOilInfo dsgOilInfo) {
        Page page = getPage();  // 分页
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("createTime");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        dsgOilInfo.setEntCode(SingletonLoginUtils.getSystemEntCode());
        IPage<DsgOilInfo> dsgOilInfoIPage = dsgOilInfoService.selectByList(page, dsgOilInfo);
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilInfoIPage.getRecords(), (int) page.getTotal()); // 格式要返回的数据
        return ajaxResult;
    }

    /**
     * 查询单条数据
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}/view")
    @ApiOperation("查询单条数据")
    public AjaxResult view(@PathVariable String id) {
        DsgOilInfo dsgOilInfo = dsgOilInfoService.getById(id);
        return json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilInfo);
    }

    /**
     * 新增
     *
     * @param dsgOilInfo
     * @return
     */
    @NoRepeatSubmit
    @PostMapping(value = "/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody DsgOilInfo dsgOilInfo) {
        if (StringUtil.isEmpty(dsgOilInfo.getOilComNo())) {
            return setErrorJson("油公司編號不能為空！");
        }
        //查询油公司编号是否存在
        QueryWrapper<DsgOilInfo> entityWrapper = new QueryWrapper<>();
        entityWrapper.eq("OIL_COM_NO", dsgOilInfo.getOilComNo());
        entityWrapper.eq("ENT_CODE", SingletonLoginUtils.getSystemEntCode());
        if (dsgOilInfoService.count(entityWrapper) > 0) {
            return setErrorJson("油公司編號：" + dsgOilInfo.getOilComNo() + "已存在！");
        }
        dsgOilInfo.setOid(UUIDGenerator.getUUID());
        boolean flag = dsgOilInfoService.save(dsgOilInfo);
        if (flag) {
            return json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilInfo);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 更新
     *
     * @param dsgOilInfo
     * @return
     */
    @NoRepeatSubmit
    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgOilInfo dsgOilInfo) {
        AjaxResult ajaxResult;
        if (StringUtil.isEmpty(dsgOilInfo.getOilComNo())) {
            ajaxResult = setErrorJson("油公司編號不能為空！");
        } else {
            //查询油公司编号是否存在
            QueryWrapper<DsgOilInfo> entityWrapper = new QueryWrapper<>();
            entityWrapper.eq("OIL_COM_NO", dsgOilInfo.getOilComNo());
            entityWrapper.eq("ENT_CODE", SingletonLoginUtils.getSystemEntCode());
            entityWrapper.ne("OID", dsgOilInfo.getOid());
            if (dsgOilInfoService.count(entityWrapper) > 0) {
                ajaxResult = setErrorJson("油公司編號：" + dsgOilInfo.getOilComNo() + ",已经存在！");
            } else {
                boolean flag = dsgOilInfoService.updateById(dsgOilInfo);
                if (flag) {
                    ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilInfo);
                } else {
                    ajaxResult = result(MessageConstants.SSO_STATUS_FAIL);
                }
            }
        }
        return ajaxResult;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation("批量删除")
    public AjaxResult delete(@ApiParam(value = "id以，號分隔", name = "idList", required = true) @RequestParam String idList) {
        if (StringUtil.isEmpty(idList)) {
            return setErrorJson("主鍵不能為空！");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("PID", Arrays.asList(idList.split(",")));
        if (dsgOilCardtypeService.count(queryWrapper) > 0) {
            return setErrorJson("油咭類型已存在，不能刪除！");
        } else {
            if (dsgOilAccountService.count(queryWrapper) > 0) {
                return setErrorJson("賬戶已存在，不能刪除！");
            } else {
                Boolean flag = dsgOilInfoService.removeByIds(Arrays.asList(idList.split(",")));
                if (flag) {
                    return result(MessageConstants.SSO_STATUS_SUCCESS);
                } else {
                    return result(MessageConstants.SSO_STATUS_FAIL);
                }
            }
        }
    }

    /**
     * 查詢油公司卡類型
     *
     * @return
     */
    @GetMapping(value = "/listCardType")
    @ApiOperation("查詢油公司卡類型")
    public AjaxResult listCardType(DsgOilCardtype dsgOilCardtype) {
        dsgOilCardtype.setEntCode(SingletonLoginUtils.getSystemEntCode());
        List<DsgOilCardtype> list = dsgOilInfoService.getDsgOilCardType(dsgOilCardtype);
        return json(MessageConstants.SSO_STATUS_SUCCESS, list, list.size());
    }
}
